//
//  CZBasicTableViewController.swift
//  swiftweibo
//
//  Created by Apple on 15/11/23.
//  Copyright © 2015年 tzw. All rights reserved.
//

import UIKit

class CZBasicTableViewController: UITableViewController {

  // 用户登录的标志
    var userLogin = CZUserAccount.userLogin
    override func loadView() {
        // 如果登录则调用父类的否则创建view
        userLogin ? super.loadView() : setupVistorView()
    }
    func setupVistorView(){
        
    // 创建view
        view = vistorView
        // 设置代理 
        vistorView.delegate = self;
        // 判断当前是，哪个控制器
        if self is CZHomeViewController{
            // 转轮转起来
            vistorView.rotationAnimation()
            // 当程序进入后台的时候 
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
            // 前台
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
        
        }else if self is CZMessageViewController{
            // 设置访客的内容
            vistorView.setupVistorView("visitordiscover_image_message", message: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
        
        }else if self is CZDiscoverViewController{
            vistorView.setupVistorView("visitordiscover_image_message", message: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
        
        }else if self is CZProfileViewController
        {
            vistorView.setupVistorView("visitordiscover_image_profile", message: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
        // 设置导航栏按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorRegisterClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style:UIBarButtonItemStyle.Plain , target: self, action: "vistorLoginClick")
    }
    
    
    // MARK: - 监听通知
    func didEnterBackground(){
        // 停止动画
        vistorView.pauseAnimation()
    }
    func didBecomeActive(){
        // 开启动画
        vistorView.resumeAnimation()
    }
    // 移除通知
    deinit
    {
        if self is CZHomeViewController{
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }
    }
    // MARK - 懒加载访客视图
    private lazy var vistorView:CZVistorView = CZVistorView()

   }
// MARK: 类的扩展
extension CZBasicTableViewController: CZVistorViewDelegate{
    // MARK:实现登录代理方法
   
    func vistorLoginClick() {
        // 创建授权界面
        let oauth = CZOauthController()
        presentViewController(UINavigationController(rootViewController: oauth), animated: true, completion: nil)
        
    }
    // 注册的代理方法
    func vistorRegisterClick() {
        print("点击了注册按钮")
    }
}